using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZYCommon;
using Bifrost;
using TextEditor.TextDocument.Document;

namespace TextEditor
{
    public partial class SelectForm : DevComponents.DotNetBar.Office2007Form
    {
        //public SelectForm(string conditionTxt)
        //{
        //    this.txtConditions.Text  = conditionTxt;
        //}

        private ArrayList items = null;

        public ArrayList Items
        {
            get { return items; }
            set { items = value; }
        }
        private KB_Item item = null;

        private KB_List kbList = null;//new KB_List();

        public KB_List KbList
        {
            get { return kbList; }
            set { kbList = value; }
        }
        public KB_Item Item
        {
            get { return item; }
            set { item = value; }
        }

        public SelectForm()
        {
            InitializeComponent();
        }

        public SelectForm(ZYTextSelect select)
        {
            InitializeComponent();
            this.txtConditions.Text = select.Value;

        }

        public ArrayList  GetALLKbItems(IDataReader myReader)
        {      
           Items = new ArrayList();     
            try
            {
                treeView1.Nodes.Clear();
                while (myReader.Read())
                {
                    KB_Item KbItem = new KB_Item();
                    KbItem.ItemSEQ=Convert.ToInt32(myReader.GetString(0));
                    KbItem.ItemValue = myReader.GetString(1);
                    TreeNode tn = new TreeNode();
                    tn.Tag = KbItem;
                    tn.Text = KbItem.ItemValue;
                    //tn.ImageIndex = this.imageList1.
                    tn.ImageIndex = 0;
                    treeView1.Nodes.Add(tn);
                    items.Add(KbItem);
                }
            }
            catch
            {
                return null; 
            }
            return Items;                   
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            this.treeView1.Nodes.Clear();
            //ZYKBBuffer buffer = ZYKBBuffer .Instance;
            //ZYDBConnection cmd = buffer.Connection;
            //IDbCommand myCmd = cmd.CreateCommand();
            //myCmd.CommandText = "select * from table_1";
            //bool b = this.txtConditions.Text.Trim().Equals("");
            //if (this.txtConditions.Text != null && !(this.txtConditions.Text.Trim() .Equals ( "")))
            //{
            //    myCmd.CommandText += " where name like '%" + this.txtConditions .Text + "%'";
            //}
            string SQl = "select * from medicine";
            if (this.txtConditions.Text != null && !(this.txtConditions.Text.Trim().Equals("")))
            {
                SQl += " where name like '%" + this.txtConditions.Text + "%'";
            }           
            DataSet ds = App.GetDataSet(SQl);
            GetALLKbItems(ds.CreateDataReader(ds.Tables[0]));//myCmd.ExecuteReader()
            
        }

        public  void treeView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                ZYTextSelect select = this.CurrentSelect;
                
               // this .MyOwnerDocument ._dele
                KB_Item tempitem = (KB_Item)treeView1.SelectedNode.Tag;
                tempitem.ItemText = tempitem.ItemValue;
                string tempSign = "";
                string tempValue = "";
                if (select != null)
                {
                    //this.MyOwnerDocument.RefreshElement(select);
                    tempSign = select.Sign;
                    tempValue= select .Text ;
                    this.MyOwnerDocument.Content.DeleteCurrentElement();
                    //select.Value = tempitem.ItemValue;
                    //for (int i = 0; i < select.ChildElements.Count; i++)
                    //{
                    //    this.MyOwnerDocument.Content.Elements.Remove(select.ChildElements[i]);
                    //}
                 
                    //this.MyOwnerDocument.Content.Elements.Remove(select);
                    //this.MyOwnerDocument.Content.InsertElement(select);
                }
                //else
                //{
                    KB_List kb_list = new KB_List();
                    kb_list.Name = "ҩƷ";
                    kb_list.Items = items;
                    kb_list.SEQ = "YP";
                    kb_list.TempValue = tempValue;
                    kb_list.TempSign = tempSign;
                    this.MyOwnerDocument._InsertKB(kb_list, tempitem);
                    // this.MyOwnerDocument._InsertKB(tempitem, 1);
               // }
               // this.MyOwnerDocument ._InsertKB(false);
                    this.Visible = false;
            }
        }

        private ZYTextDocument myOwnerDocument = null;

        public ZYTextDocument MyOwnerDocument
        {
            get { return myOwnerDocument; }
            set { myOwnerDocument = value; }
        }

        private KB_Item currentItem = null;

        public KB_Item CurrentItem
        {
            get { return currentItem; }
            set { currentItem = value; }
        }

        private ZYTextSelect currentSelect = null;

        public ZYTextSelect CurrentSelect
        {
            get { return currentSelect; }
            set { currentSelect = value; }
        }

       

        private void btnSelect_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this .txtConditions  = null;
                this.Close();
            }
            else if (e.KeyCode == Keys.Enter)
            {
                
            }
        }

        public void CloseList()
        {
            base.Hide();
           // base.Dispose();
        }

        private void SelectForm_Load(object sender, EventArgs e)
        {

        }
    }
}